﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.PhongMau
{
    public partial class Phongmau_Themnhomhang : Form
    {
        public static string MaID = "0";
        public bool update = false;

        public Phongmau_Themnhomhang()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            if (MaID!="0")
            {
                var data = DataAll.SelectAll("PhongMau_NhomHang", "*", " WHERE IdNhomHang=" + MaID);
                txtMaNhom.Text = data.Rows[0]["MaNhom"].ToString();
                txtTenNhom.Text = data.Rows[0]["TenNhom"].ToString();
                txtGhiChu.Text = data.Rows[0]["GhiChu"].ToString();
            }
        }

        public void ResetControl()
        {
            txtMaNhom.Text = string.Empty;
            txtTenNhom.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
        }
        
        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            if (update)
            {
                var message = DataAll.Update("PhongMau_NhomHang", "MaNhom=N'" + txtMaNhom.Text + "', TenNhom=N'" + txtTenNhom.Text
                    + "', GhiChu=N'" + txtGhiChu.Text + "' WHERE IdNhomHang="+MaID);
                MessageBox.Show(message.Equals("success") ? "Sửa nhóm hàng thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Close();
            }
            else
            {
                var message = DataAll.InsertAll("PhongMau_NhomHang", txtMaNhom.Text + "," + txtTenNhom.Text + "," + txtGhiChu.Text);
                MessageBox.Show(message.Equals("success") ? "Thêm nhóm hàng thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                ResetControl();
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
